package com.ym.jiujiangeyou.parse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.ym.jiujiangeyou.entity.Food;
import com.ym.jiujiangeyou.entity.Guide;
import com.ym.jiujiangeyou.entity.Hotel;
import com.ym.jiujiangeyou.entity.InstallMessage;
import com.ym.jiujiangeyou.entity.Scenery;
import com.ym.jiujiangeyou.util.Constants;
 /**   
 * @author 叶铭   
 * @email yeming_1001@163.com
 * @version V1.0   
 * @Description: 解析Gson数据  
 */
public class ParseGson {

	public ParseGson() {
	}

	
	/** 
	* @param str
	* @retur  Map<String,Object> 
	* @Description: 解析scenery guide数据 
	*/
	public static Map<String,Object> parseSceneryGuide(String str){
		Gson gson ;
		List<Guide> guides ;
		List<Scenery> scenerys ;
		Map<String,Object> map = new HashMap<String, Object>() ;
		int status = 0 ;
		if(str != null && str.equals(Constants.CONNECTION_TIMEOUT)){
			map.put("status", Constants.LISTVIEW_ACTION_GET_ERROR) ;
			return map ;
		}
		gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
				.create();
		SceneryAndGuideList list = null ;
		try {
			list = gson.fromJson(str, SceneryAndGuideList.class);
		} catch (JsonSyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put("status", Constants.LISTVIEW_ACTION_GET_ERROR) ;
			return map ;
		}
		status = list.getStatus() ;
		//System.out.println("---------status----------"+status) ;
		if(status == 0){
			map.put("status", status) ;
		}else if(status == 1){
			guides = list.getGuide() ;
			scenerys = list.getScenery() ;
			//System.out.println("---------size-------"+guides.size()+"--"+scenerys.size()) ;
			map.put("status", status) ;
			map.put("guide", guides) ;
			map.put("scenery", scenerys) ;
		}
		return map ;
	}
	
	/** 
	* @param str
	* @return  
	* @retur  Map<String,Object> 
	* @Description: 解析酒店信息数据 
	*/
	public static Map<String,Object> parseHotel(String str){
		Gson gson ;
		List<Hotel> hotel = null ;
		Map<String,Object> map = new HashMap<String, Object>() ;
		int status = 0 ;
		if(str != null && str.equals(Constants.CONNECTION_TIMEOUT)){
			map.put("status", Constants.LISTVIEW_ACTION_GET_ERROR) ;
			return map ;
		}
		gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
				.create();
		HotelList hotelList = null ;
		try {
			hotelList = gson.fromJson(str, HotelList.class);
		} catch (JsonSyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put("status", Constants.LISTVIEW_ACTION_GET_ERROR) ;
			return map ;
		}
		status = hotelList.getStatus() ;
		if(status == 0){
			map.put("status", status) ;
		}else if(status == 1){
			hotel = hotelList.getHotel() ;
		//	System.out.println("---------size-------"+hotel.size()) ;
			map.put("status", status) ;
			map.put("hotel", hotel) ;
		}
		return map ;
	}
	
	
	/** 
	* @param str
	* @return  
	* @retur  Map<String,Object> 
	* @Description: pic url  解析 
	*/
	public static Map<String,Object> parsePic(String str){
		Gson gson ;
		List<String> pics = null ;
		Map<String,Object> map = new HashMap<String, Object>() ;
		int status = 0 ;
		if(str != null && str.equals(Constants.CONNECTION_TIMEOUT)){
			map.put("status", Constants.LISTVIEW_ACTION_GET_ERROR) ;
			return map ;
		}
		if(str.equals("FAIL")){
			map.put("status", Constants.LISTVIEW_ACTION_GET_ERROR) ;
			return map ;
		}
		gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
				.create();
		PicUrlList urlList = null ;
		try {
			urlList = gson.fromJson(str, PicUrlList.class);
		} catch (JsonSyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put("status", Constants.LISTVIEW_ACTION_GET_ERROR) ;
			return map ;
		}
		status = urlList.getStatus() ;
		if(status == 0){
			map.put("status", status) ;
		}else if(status == 1){
			pics = urlList.getPicurl();
			//System.out.println("---------size-------"+pics.size()) ;
			map.put("status", status) ;
			map.put("pics", pics) ;
		}
		return map ;
	}
	
	/** 
	* @param str
	* @return  
	* @retur  Map<String,Object> 
	* @Description: 解析美食信息 
	*/
	public static Map<String,Object> parseFood(String str){
		Gson gson ;
		List<Food> food = null ;
		Map<String,Object> map = new HashMap<String, Object>() ;
		int status = 0 ;
		if(str != null && str.equals(Constants.CONNECTION_TIMEOUT)){
			map.put("status", Constants.LISTVIEW_ACTION_GET_ERROR) ;
			return map ;
		}
		gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
				.create();
		FoodList foodList = null ;
		try {
			foodList = gson.fromJson(str, FoodList.class);
		} catch (JsonSyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put("status", Constants.LISTVIEW_ACTION_GET_ERROR) ;
			return map ;
		}
		status = foodList.getStatus() ;
		if(status == 0){
			map.put("status", status) ;
		}else if(status == 1){
			food = foodList.getFood();
			//System.out.println("---------size-------"+food.size()) ;
			map.put("status", status) ;
			map.put("food", food) ;
		}
		return map ;
	}
	
	/** 
	* @param result
	* @return  
	* @retur  InstallMessage 
	* @Description: 解析跟新信息数据
	*/
	public static InstallMessage parseInstallMessage(String result){
		Gson gson ;
		InstallMessage installMessage = null ;
		if(result != null && result.equals(Constants.CONNECTION_TIMEOUT)){
			return installMessage ;
		}
		gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
				.create();
		try {
			installMessage = gson.fromJson(result,InstallMessage.class) ;
		} catch (JsonSyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			installMessage = null ;
			return installMessage ;
		}
		return installMessage ;
	}
}
